package com.tencent.welink.api.msg;

import com.alibaba.fastjson.JSONObject;
import com.tencent.welink.api.OpenApiTokenChange;
import com.tencent.welink.api.base.WelinkOpenApi;
import com.tencent.welink.config.BussinessConfig;
import com.tencent.welink.util.CrontabUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.UUID;

/**
 * Created by junchihe on 2020/6/4.
 * desc:消息广播接口，发送消息到平台。
 */
public class MessageReport extends WelinkOpenApi {
    private static final Logger log = LogManager.getLogger(MessageReport.class);

    public MessageReport() {
        super("/common/msg/report", false);
    }

    @Override
    public void displayResult(String result) {

    }

    /**
     * 1000100，1000101，1000201，1000500
     * message_type	说明
     * 1000100	文本消息上报至平台作为数据存储
     * 1000101	Json 消息上报至平台作为数据存储
     * 1000200	发送文本消息至其他应用系统
     * 1000201	发送 Json 消息至其他应用系统
     * 1000300	推送告警类消息给微瓴平台，不直接推送给应用，微瓴平台会针对告警消息的业务类别推送给相应的业务应用
     * 1000302	推送告警类消息给微瓴平台，不直接推送给应用，微瓴平台会针对告警消息的业务类别推送给相应的业务应用
     * 1000400	推送通知类消息给微瓴平台，不直接推送给应用，微瓴平台会针对通知消息的业务类别推送给相应的业务应用
     * 1000500	推送普通消息给微瓴平台作为数据存储，不推送给应用
     *
     * @param args
     */
    public static void main(String[] args) {
        new BussinessConfig();
        CrontabUtil.addTaskInRate(new OpenApiTokenChange(), BussinessConfig.loginDurationMinutes, true);
        MessageReport messageReport = new MessageReport();
        JSONObject jsonObject = messageReport.getBodyPara();
        //请求包体:{"message_type":1000100,"content":"test content"}

        jsonObject.put("message_type", 1000100);
        jsonObject.put("content", "test adasdfasdfasdf");
        messageReport.DoHttp();

        jsonObject.clear();
        jsonObject.put("message_type", 1000101);
        JSONObject va = new JSONObject();
        jsonObject.put("content", va);
        va.put("key1", 23);
        va.put("key2", "junchihetest");
        messageReport.DoHttp();

        jsonObject.clear();
        jsonObject.put("message_type", 1000200);
        //消息属性。ack：根据之前的消息返回，active：主动发起
        jsonObject.put("msg_attribute", "active");
        jsonObject.put("to_appid", 41149);
        jsonObject.put("content", "teseeedddadfsdf");

        jsonObject.clear();
        jsonObject.put("message_type", 1000201);
        //消息属性。ack：根据之前的消息返回，active：主动发起
        jsonObject.put("msg_attribute", "active");
        jsonObject.put("to_appid", 1212);
        jsonObject.put("content", va);
        messageReport.DoHttp();

        jsonObject.clear();
        jsonObject.put("message_type", 1000300);
        jsonObject.put("content", fireAlarm("300000000000073213"));
        messageReport.DoHttp();

        jsonObject.clear();
        jsonObject.put("message_type", 1000302);
        jsonObject.put("content", "warning");
        messageReport.DoHttp();

        jsonObject.clear();
        jsonObject.put("message_type", 1000400);
        jsonObject.put("content", "notify");
        messageReport.DoHttp();

        jsonObject.clear();
        jsonObject.put("message_type", 1000500);
        jsonObject.put("content", "planning");
        messageReport.DoHttp();


        //parking/in
        jsonObject.clear();
        jsonObject.put("message_type", 1000400);
        jsonObject.put("content", sendParkingInMsg("300000000000073213"));
        messageReport.DoHttp();

        CrontabUtil.shutdown();

    }

    /**
     * parking/in
     */
    public static String sendParkingInMsg(String din) {
        String value = "{" +
                "\"id\": \"" + UUID.randomUUID() + "\"," +
                "\"time\": \"1593401660973\"," +
                "\"type\": \"parking\"," +
                "\"sub_type\": \"in\"," +
                "\"position\": {" +
                "\"din\": \"" + din + "\"" +
                "}," +
                "\"description\": \"\"," +
                "\"image\": {" +
                "" +
                "}," +
                "\"extend\": {" +
                "\"laneType\": 1," +
                "\"carType\": 1," +
                "\"parkingLotId\": 0," +
                "\"permission\": 1," +
                "\"plateNum\": \"B12345\"," +
                "\"vehicleType\": 1," +
                "\"parkingLotName\": \"停车场2\"," +
                "\"exitName\": \"出口1\"" +
                "}" +
                "}";
        return value;
    }

    /**
     * 火警
     *
     * @param din
     * @return
     */
    public static String fireAlarm(String din) {
        String content = "{" +
                "\"id\":\"" + UUID.randomUUID() + "\"," +
                "\"time\":\"123454678000\"," +
                "\"type\":\"fire_protecting\"," +
                "\"sub_type\":\"fire_alarm\"," +
                "\"level\":  5," +
                "\"position\": {" +
                "\"din\":\"" + din + "\"," +
                "\"wId\":\"\"," +
                "\"text\": \"\"" +
                "}," +
                "\"description\": \"xxx\"," +
                "\"advice\":\"xxx\"," +
                "\"handler\": {" +
                "\"type\":\"\"," +
                "\"data\":\"\"" +
                "}," +
                "\"status\": \"processed\"," +
                "\"processor\": []," +
                "\"image\": {}," +
                "\"extend\": {}" +
                "}";
        return content;
    }
}
